<template>
  <div class="login-container">
    <!-- logo -->
    <div class="box">
      <div class="logo">
        <a href="">
          <img src="http://8.142.74.15:9092/img/logo.f558a495.svg" alt="">
          <span>进销存</span>
        </a>
      </div>
      <div class="text">开放，合作，更快更好更卓越......</div>
    </div>
    <!-- 输入框 -->
    <template class="login-form">
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <!-- 账号密码登录 -->
        <el-tab-pane label="账号密码登录" name="first">

          <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item prop="username" class="username">
              <i class="el-icon-user"></i>
              <el-input v-model="ruleForm.username" placeholder="请输入用户名"></el-input>
            </el-form-item>
            <el-form-item prop="password" class="password">
              <i class="el-icon-lock"></i>
              <el-input v-model="ruleForm.password" show-password placeholder="请输入密码"></el-input>
            </el-form-item>
            <el-form-item prop="captcha" class="captcha">
              <i class="el-icon-s-opportunity"></i>
              <el-input v-model="ruleForm.captcha" placeholder="请输入验证码"></el-input>
              <img @click="getcaptcha()" :src="url" alt="">
            </el-form-item>
            <el-form-item>
              <el-button class="login" type="primary" @click="submitForm()">确定</el-button>
            </el-form-item>
          </el-form>

        </el-tab-pane>
        <!-- 手机号登录 -->
        <el-tab-pane label="手机号登录" name="second">

          <el-form :model="ruleForm2" :rules="rules" ref="ruleForm2" label-width="100px" class="demo-ruleForm">
            <el-form-item prop="mobile" class="mobile">
              <i class="el-icon-mobile"></i>
              <el-input v-model="ruleForm2.mobile" placeholder="请输入手机号"></el-input>
            </el-form-item>
            <el-form-item prop="captcha" class="captcha">
              <i class="el-icon-message"></i>
              <el-input v-model="ruleForm2.captcha" placeholder="请输入验证码"></el-input>
              <el-button plain class="sjyzm" @click="getsjcaptcha()">获取验证码</el-button>
            </el-form-item>
            <el-form-item>
              <el-button class="login" type="primary" @click="sjlogin()">确定</el-button>
            </el-form-item>
          </el-form>

        </el-tab-pane>
      </el-tabs>
    </template>
  </div>
</template>

<script>
import { Login, captcha, sjcaptcha, sjlogin } from '@/api/user';
export default {
  data() {
    return {
      activeName: 'first',
      url: '',
      ruleForm: {
        captcha: "",
        checkKey: null,
        password: "admin@Erp.",
        remember_me: true,
        username: "admin",
      },
      ruleForm2: {
        mobile: '',
        captcha: '',
        remember_me: true
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
        ],
        captcha: [
          { required: true, message: '请输入验证码', trigger: 'blur' },
        ],
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              const phonePattern = /^1[3-9]\d{9}$/;
              if (!phonePattern.test(value)) {
                callback(new Error('请输入正确的手机号'));
              } else {
                callback();
              }
            },
            trigger: 'change'
          }
        ],
      }
    }
  },
  mounted() {
    this.getcaptcha();
  },
  watch: {
  },
  methods: {
    async getcaptcha() {
      let newdate = new Date().getTime();
      this.ruleForm.checkKey = newdate;
      let data = {
        shijian: newdate,
        num: Math.floor(newdate / 1000)
      };
      let res = await captcha(data);
      this.url = res.result;
    },
    async getsjcaptcha() {
      // mobile 字段进行前置验证
      this.$refs['ruleForm2'].validateField('mobile', async (valid) => {
        console.log(valid,888)
        if (!valid) {
          // 验证通过
          let res = await sjcaptcha({
            mobile: this.ruleForm2.mobile,
            smsmode: "0"
          });
          this.$Myprompt(res)
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    sjlogin() {
      this.$refs['ruleForm2'].validate(async (valid) => {
        if (valid) {
          let res = await sjlogin(this.ruleForm2);
          this.$Myprompt(res)
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    submitForm() {
      this.$refs['ruleForm'].validate(async (valid) => {
        if (!valid) return
        let res = await Login(this.ruleForm)
        console.log(res)
        this.$Myprompt(res)
        if(res.code==200){
          localStorage.setItem('Token', res.result.token)
          this.$router.push('analysis')
        }
      });
    },
    handleClick(tab, event) {
      console.log(tab, event);
    },
  }
}
</script>

<style lang="less" scoped>
* {
  margin: 0;
  padding: 0;
}

a {
  text-decoration: none;
}

// element组件选择登录方式
.el-tabs {
  min-width: 260px;
  width: 368px;
  margin: 0 auto;
}

::v-deep .el-tabs__nav-wrap::after {
  background-color: #f0f2f5;
}

::v-deep .el-tabs__header {
  padding: 0 18%;
}

::v-deep .el-form-item__content {
  margin-left: 0 !important;
  margin-bottom: 24px;
  position: relative;
}

::v-deep .el-input__inner {
  padding: 0 15px 0 32px;
}

::v-deep .captcha .el-input__inner {
  width: 65%;
}

// ::v-deep.el-tabs__nav{
//     position: relative;
//     display: inline-block;
//     box-sizing: border-box;
//     margin: 0;
//     padding-left: 0;
//     list-style: none;
//     transition: transform .3s cubic-bezier(.645,.045,.355,1);
// }

// 主体
.login-container {
  width: 100%;
  min-height: 100%;
  background: #f0f2f5 url(http://8.142.74.15:9092/img/background.5825f033.svg) no-repeat 50%;
  background-size: 100%;
  padding: 110px 0 144px;
  position: relative;

  // 登录logo
  .box {
    text-align: center;

    .logo {
      height: 44px;
      line-height: 44px;

      img {
        height: 44px;
        vertical-align: top;
        margin-right: 16px;
        border-style: none;
      }

      span {
        font-size: 33px;
        color: rgba(0, 0, 0, .85);
        font-weight: 600;
        position: relative;
        top: 2px;
      }
    }

    .text {
      font-size: 14px;
      color: rgba(0, 0, 0, .45);
      height: 21px;
      margin-top: 12px;
      margin-bottom: 40px;
    }
  }

  // 输入框
  .captcha {
    position: relative;

    img {
      position: absolute;
      right: 10px;
      bottom: 3px;
    }
  }

  .el-form-item__content {
    i {
      position: absolute;
      z-index: 10;
      top: 13px;
      left: 11px;
      color: #bfbfbf;
    }
  }

  .login {
    padding: 0 15px;
    font-size: 16px;
    height: 40px;
    width: 100%;
  }

  // 获取验证码
  .sjyzm {
    width: 31%;
    height: 40px;
    position: absolute;
    right: 0px;
    bottom: 1px;
  }
}
</style>
